#include<iostream>
#include<fstream>
#include<contacts.pb.h>

using namespace std;

void PrintContacts(contacts::Contacts& contacts)
{
    for(int i = 0; i < contacts.contacts_size(); i++)
    {
        cout<<"---------联系人"<< i+1 << "------------"<<endl;
        const ::contacts::PeopleInfo& people = contacts.contacts(i);
        cout << "name:" << people.name() << endl;
        cout << "age:" << people.age() << endl;
        for(int j = 0;j < people.phone_size();j++)
        {
           const ::contacts::PeopleInfo_Phone& phone = people.phone(j);
           cout << "phone number"<< j+1 << ":" << phone.number() << "(" << phone.PhoneType_Name(phone.type()) <<")" <<endl;
        }

        if(people.has_data() && people.data().Is<contacts::Address>())
        {
            contacts::Address address;
            people.data().UnpackTo(&address);
            if(!address.home_address().empty())
                cout << "联系人家庭地址：" << address.home_address() << endl;
            if(!address.unit_address().empty())
                cout << "联系人单位地址：" << address.unit_address() << endl;
        }

        switch(people.other_contact_case())
        {
            case contacts::PeopleInfo::OtherContactCase::kQq:
            cout << "联系人qq：" << people.qq() << endl; 
            break;
            case contacts::PeopleInfo::OtherContactCase::kWechat:
            cout << "联系人qq：" << people.qq() << endl; 
            break;
        }

        if(people.remark_size())
        {
            cout << "备注信息：" << endl;
            for(auto it = people.remark().cbegin();it != people.remark().end();it++)
            {
                cout << "    " << it->first << ":" << it->second << endl;
            }
        }

    }
}
int main()
{
    contacts::Contacts contacts;
    //读取本地已存在的通讯录文件
    fstream input("contacts.bin",ios::in | ios::binary);
    if(!input)
    {
        cout << "contacts.bin not find,creat new file!" << endl;
    }else if(!contacts.ParseFromIstream(&input)){
        cerr << "parse error!" << endl;
        input.close();
        return -1;
    }


    PrintContacts(contacts);
    input.close();
    return 0;
}